function ListNode(val, next) {
  this.val = (val===undefined ? 0 : val)
  this.next = (next===undefined ? null : next)
}
function createList(arr) {
  if(arr.length == 0)
    return null;
  let head = new ListNode(arr[0], null), headNode = head;
  for(let i = 1; i < arr.length; i++) {
    let temp = new ListNode(arr[i], null);
    head.next = temp;
    head = temp;
  }
  return headNode;
}
function showList(head) {
  while(head) {
    console.log(head.val);
    head = head.next;
  }
}
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
  if(head == null)
    return head;
  let newHeader = new ListNode(head.val, null);
  head = head.next;
  while(head) {
    let tempNode = head.next
    head.next = newHeader;
    newHeader = head;
    head = tempNode;
  }
  return newHeader;
};
let head = [1,2,3,4,5];
console.log(reverseList(head));
head = [1,2];
console.log(reverseList(head));
head = [];
console.log(reverseList(head));
